{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### pry-docの読み込み\n",
    "require \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-doc-0.10.0/lib/pry-doc\"\n",
    "#require \"/root/git_jupyter_notebook/Ruby/vendor/bundle/ruby/2.3.0/gems/pry-doc-0.10.0/lib/pry-doc\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Stringクラス\n",
    "\n",
    "* Stringクラス\n",
    "    * 文字列はStringクラスのオブジェクト\n",
    "    * Ruby1.9より文字コード(エンコーディング情報)がStringオブジェクトに追加されている\n",
    "        * Stringオブジェクトのメソッドは1文字を単位として動作するようになった\n",
    "    * メソッド\n",
    "        * encoding\n",
    "            * エンコーディング情報の取得\n",
    "            * Stringクラスのインスタンスメソッド\n",
    "            * <font color=\"red\">戻り値はEncofingクラスのインスタンスになる</font>\n",
    "        * encode\n",
    "            * 引数で指定した文字コードに変換した新しいインスタンスを返す\n",
    "        * encode!\n",
    "            * オブジェクトのエンコーディングを変更する\n",
    "            * 破壊的メソッド\n",
    "        \n",
    "主なエンコーディング\n",
    "\n",
    "|Encoding|概要|\n",
    "|:---:|:---:|\n",
    "|Encoding::UTF_8 |UTF-8を表すエンコーディング |\n",
    "|Encoding::EUC_JP |EUC-JPを表すエンコーディング |\n",
    "|Encoding::ISO_2022_JP |JISを表すエンコーディング |\n",
    "|Encoding::Shif_JIS |Shift_JISを表すエンコーディング |\n",
    "|Encoding::Windows_31J |Windowsで用いられるShift_JISの亜種。CP932ともいわれる |\n",
    "|Encoding::ASCII |US-ASCIIを表すエンコーディング。Encoding::US_ASCIIでも参照できる |\n",
    "|Encoding::ASCII_8BIT |ASCII互換エンコーディング。バイト列として扱いたい時に利用 |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "デフォルトのエンコーディング\n",
      "#<Encoding:UTF-8>\n",
      "\n",
      "エンコーディングの変更\n",
      "\"\\u3042\"\n",
      "UTF-8\n",
      "\"\\x{82A2}\"\n",
      "Shift_JIS\n"
     ]
    }
   ],
   "source": [
    "puts \"デフォルトのエンコーディング\"\n",
    "p \"あ\".encoding\n",
    "puts\n",
    "\n",
    "puts \"エンコーディングの変更\"\n",
    "p \"あ\"\n",
    "puts \"あ\".encoding\n",
    "p \"い\".encode(Encoding::Shift_JIS)\n",
    "puts \"い\".encode(Encoding::Shift_JIS).encoding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Unicodeのコードポイントから文字列を作成\n",
      "あいう\n"
     ]
    }
   ],
   "source": [
    "puts \"Unicodeのコードポイントから文字列を作成\"\n",
    "puts \"\\u3042\\u3044\\u3046\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Stringクラスの継承チェーン"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[String, JSON::Ext::Generator::GeneratorMethods::String, Comparable, Object, PP::ObjectMixin, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "String.ancestors"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## encodingメソッドの挙動"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abc\n",
      "#<Encoding:UTF-8>\n",
      "文字列はStringクラス、encodingメソッドの戻り値はEncodingクラス\n",
      "String\n",
      "Encoding\n"
     ]
    }
   ],
   "source": [
    "a = \"abc\"\n",
    "puts a\n",
    "p a.encoding\n",
    "puts \"文字列はStringクラス、encodingメソッドの戻り値はEncodingクラス\"\n",
    "puts a.class\n",
    "puts a.encoding.class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m encoding.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m String\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m encoding()\n",
      "\u001b[1mNumber of lines:\u001b[0m 1\n",
      "\n",
      "Returns the Encoding object that represents the encoding of obj.\n"
     ]
    }
   ],
   "source": [
    "show-doc String#encoding"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## encodeとencode!メソッド"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "encodeメソッドは元のインスタンスは変更なし\n",
      "EUC-JP\n",
      "UTF-8\n",
      "\n",
      "encode!メソッドは元のインスタンスの文字コードを変更する\n",
      "EUC-JP\n",
      "EUC-JP\n"
     ]
    }
   ],
   "source": [
    "a = \"abc\"\n",
    "\n",
    "puts \"encodeメソッドは元のインスタンスは変更なし\"\n",
    "puts a.encode(\"EUC-JP\").encoding\n",
    "puts a.encoding\n",
    "puts \"\"\n",
    "\n",
    "puts \"encode!メソッドは元のインスタンスの文字コードを変更する\"\n",
    "puts a.encode!(\"EUC-JP\").encoding\n",
    "puts a.encoding"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Encodingクラス\n",
    "\n",
    "* エンコーディング情報を扱うクラス\n",
    "* メソッド\n",
    "    * default_external\n",
    "        * 規定の外部エンコーディングを取得\n",
    "            * Linuxであればlocaleの設定\n",
    "                * dockerの公式Ubuntuイメージを使うとUS-ASCII\n",
    "                * 日本語でUTF-8が設定されている環境であればUTF-8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Encoding, Object, PP::ObjectMixin, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]\n",
      "\n",
      "docker公式Ubuntuの環境そのまま\n",
      "#<Encoding:US-ASCII>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "#<Encoding:US-ASCII>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "puts Encoding.ancestors\n",
    "puts \"\"\n",
    "\n",
    "puts \"docker公式Ubuntuの環境そのまま\"\n",
    "p Encoding.default_external"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Encodingの互換性\n",
    "\n",
    "* compatible?\n",
    "    * 異なるエンコーディング間の互換性を調べる"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "#<Encoding:UTF-8>\n",
      "\n",
      "互換性が無い場合はnilを返す\n",
      "nil\n"
     ]
    }
   ],
   "source": [
    "p Encoding.compatible?(Encoding::UTF_8, Encoding::US_ASCII)\n",
    "puts\n",
    "\n",
    "puts \"互換性が無い場合はnilを返す\"\n",
    "p Encoding.compatible?(Encoding::UTF_8, Encoding::Shift_JIS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m encoding.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m #<Class:Encoding>\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m compatible?(arg1, arg2)\n",
      "\u001b[1mNumber of lines:\u001b[0m 18\n",
      "\n",
      "Checks the compatibility of two objects.\n",
      "\n",
      "If the objects are both strings they are compatible when they are\n",
      "concatenatable.  The encoding of the concatenated string will be returned\n",
      "if they are compatible, nil if they are not.\n",
      "\n",
      "  \u001b[1;34;4mEncoding\u001b[0m.compatible?(\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[1;35m\\xa1\u001b[0m\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.force_encoding(\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31miso-8859-1\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m), \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mb\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m)\n",
      "  \u001b[1;34m#=> #<Encoding:ISO-8859-1>\u001b[0m\n",
      "\n",
      "  \u001b[1;34;4mEncoding\u001b[0m.compatible?(\n",
      "    \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[1;35m\\xa1\u001b[0m\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.force_encoding(\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31miso-8859-1\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m),\n",
      "    \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[1;35m\\xa1\u001b[0m\u001b[31m\u001b[1;35m\\xa1\u001b[0m\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.force_encoding(\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31meuc-jp\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m))\n",
      "  \u001b[1;34m#=> nil\u001b[0m\n",
      "\n",
      "If the objects are non-strings their encodings are compatible when they\n",
      "have an encoding and:\n",
      "* Either encoding is US-ASCII compatible\n",
      "* One of the encodings is a 7-bit encoding\n"
     ]
    }
   ],
   "source": [
    "show-doc Encoding.compatible?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "互換性のあるエンコーディングは結合できる\n",
      "US-ASCII\n",
      "UTF-8\n",
      "abcあいう\n",
      "\n",
      "互換性のないエンコーディングでも片方がASCII文字しか含まない場合は結合できる\n",
      "EUC-JP\n",
      "UTF-8\n",
      "abcあいう\n",
      "\n",
      "両方ともASCII文字以外かつ互換性のないエンコーディングは結合できない\n",
      "EUC-JP\n",
      "UTF-8\n",
      "incompatible character encodings: EUC-JP and UTF-8\n",
      "\n"
     ]
    }
   ],
   "source": [
    "puts \"互換性のあるエンコーディングは結合できる\"\n",
    "puts \"abc\".encode(\"ASCII\").encoding\n",
    "puts \"あいう\".encode(\"UTF-8\").encoding\n",
    "puts \"abc\".encode(\"ASCII\") + \"あいう\".encode(\"UTF-8\")\n",
    "puts \"\"\n",
    "\n",
    "puts \"互換性のないエンコーディングでも片方がASCII文字しか含まない場合は結合できる\"\n",
    "puts \"abc\".encode(\"EUC-JP\").encoding\n",
    "puts \"あいう\".encode(\"UTF-8\").encoding\n",
    "puts \"abc\".encode(\"EUC-JP\") + \"あいう\".encode(\"UTF-8\")\n",
    "puts \"\"\n",
    "\n",
    "puts \"両方ともASCII文字以外かつ互換性のないエンコーディングは結合できない\"\n",
    "puts \"あいう\".encode(\"EUC-JP\").encoding\n",
    "puts \"えお\".encode(\"UTF-8\").encoding\n",
    "begin\n",
    "  puts \"あいう\".encode(\"EUC-JP\") + \"えお\".encode(\"UTF-8\")\n",
    "rescue Encoding::CompatibilityError\n",
    "  puts $!\n",
    "end\n",
    "puts \"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 文字列の比較\n",
    "\n",
    "* メソッド\n",
    "    * ==\n",
    "        * 大文字小文字も含めて同じであるかどうか\n",
    "            * ==のみ異なるクラスのオブジェクトと比較できるが、型の変換は行われないので必ずfalseになる\n",
    "            * エンコーディングが異なっていると同じ文字列でもfalseになる\n",
    "                * ただし、ASCII互換のエンコーディングでASCIIしか含まれていない場合は同じ文字列の場合trueになる\n",
    "    * >\n",
    "        * 文字列同士をアスキーコードで比較し、結果をtrue, falseで返す\n",
    "    * `>=`\n",
    "        * 文字列同士をアスキーコードで比較し、結果をtrue, falseで返す\n",
    "    * <\n",
    "        * 文字列同士をアスキーコードで比較し、結果をtrue, falseで返す\n",
    "    * `<=`\n",
    "        * 文字列同士をアスキーコードで比較し、結果をtrue, falseで返す\n",
    "    * <=>\n",
    "        * 大文字小文字を区別する\n",
    "            * 1  左が右よりも大きい\n",
    "            * 0  左右同じ\n",
    "            * -1 左が右よりもより小さい\n",
    "    * casecmp\n",
    "        * 大文字小文字を区別しない\n",
    "        * あとは<=>と同じ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文字列が同じかどうかを比較\n",
      "true\n",
      "false\n",
      "\n",
      "アスキーコード a(97),b(98),c(99)\n",
      "97\n",
      "98\n",
      "99\n",
      "\n",
      "等号、不等号による比較\n",
      "true\n",
      "\n",
      "<=>による比較\n",
      "1\n",
      "0\n",
      "-1\n",
      "\n",
      "-1\n",
      "\n",
      "大文字、小文字を区別しない比較\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "puts \"文字列が同じかどうかを比較\"\n",
    "puts \"abc\" == \"abc\"\n",
    "puts \"Abc\" == \"abc\"\n",
    "puts \"\"\n",
    "puts \"アスキーコード a(97),b(98),c(99)\"\n",
    "puts \"a\".ord\n",
    "puts \"b\".ord\n",
    "puts \"c\".ord\n",
    "puts \"\"\n",
    "puts \"等号、不等号による比較\"\n",
    "puts \"a\" < \"b\"\n",
    "puts \"\"\n",
    "puts \"<=>による比較\"\n",
    "puts \"b\" <=> \"a\"\n",
    "puts \"b\" <=> \"b\"\n",
    "puts \"b\" <=> \"c\"\n",
    "puts\n",
    "puts \"大文字、小文字を区別しない比較\"\n",
    "puts \"aBc\".casecmp(\"AbC\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m string.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m String\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m casecmp(arg1)\n",
      "\u001b[1mNumber of lines:\u001b[0m 6\n",
      "\n",
      "Case-insensitive version of \u001b[1;34;4mString\u001b[0m\u001b[1;34m#<=>\u001b[0m.\n",
      "\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mabcdef\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.casecmp(\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mabcde\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m)     \u001b[1;34m#=> 1\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31maBcDeF\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.casecmp(\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mabcdef\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m)    \u001b[1;34m#=> 0\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mabcdef\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.casecmp(\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mabcdefg\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m)   \u001b[1;34m#=> -1\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mabcdef\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.casecmp(\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mABCDEF\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m)    \u001b[1;34m#=> 0\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "show-doc String#casecmp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 文字列の切り出し\n",
    "\n",
    "* メソッド\n",
    "    * []\n",
    "        * 文字列から指定された一部を切り出すメソッド\n",
    "        * 引数の個数や値に応じて返す結果が異なる\n",
    "    * slice\n",
    "        * 上と同じ\n",
    "    * slice!\n",
    "        * 返した文字を元の文字列から取り除く\n",
    "        * 破壊的メソッド\n",
    "    * split\n",
    "        * 文字列や正規表現を使って文字列を分割する\n",
    "* 範囲指定\n",
    "    * rangeオブジェクトを指定すると該当する範囲の文字列を返す\n",
    "    * 開始位置と長さで指定できる\n",
    "        * 開始位置が範囲外の時はnilを返す\n",
    "* 文字列で指定\n",
    "    * 元の文字列に含まれていればその部分を返す\n",
    "    * 含まれていなければnilを返す\n",
    "* 正規表現\n",
    "    * マッチした部分を返す"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]メソッド\n",
      "a\n",
      "cdef\n",
      "cde\n",
      "cd\n",
      "g\n",
      "ef\n",
      "\n",
      "sliceメソッド\n",
      "a\n",
      "cdef\n",
      "cde\n",
      "cd\n",
      "g\n",
      "ef\n",
      "\n"
     ]
    }
   ],
   "source": [
    "puts \"[]メソッド\"\n",
    "puts \"abcdefg\"[0]\n",
    "puts \"abcdefg\"[2,4]\n",
    "puts \"abcdefg\"[2..4]\n",
    "puts \"abcdefg\"[2...4]\n",
    "puts \"abcdefg\"[-1]\n",
    "puts \"abcdefg\"[-3,2]\n",
    "puts \"\"\n",
    "\n",
    "puts \"sliceメソッド\"\n",
    "puts \"abcdefg\".slice(0)\n",
    "puts \"abcdefg\".slice(2,4)\n",
    "puts \"abcdefg\".slice(2..4)\n",
    "puts \"abcdefg\".slice(2...4)\n",
    "puts \"abcdefg\".slice(-1)\n",
    "puts \"abcdefg\".slice(-3,2)\n",
    "puts \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "開始位置が範囲外\n",
      "g\n",
      "nil\n",
      "a\n",
      "nil\n",
      "\n",
      "文字列で指定\n",
      "cde\n",
      "nil\n",
      "\n"
     ]
    }
   ],
   "source": [
    "puts \"開始位置が範囲外\"\n",
    "puts \"abcdefg\"[6]\n",
    "p \"abcdefg\"[7]\n",
    "puts \"abcdefg\"[-7]\n",
    "p \"abcdefg\"[-8]\n",
    "puts \"\"\n",
    "\n",
    "puts \"文字列で指定\"\n",
    "puts \"abcdefg\"[\"cde\"]\n",
    "p \"abcdefg\"[\"df\"]\n",
    "puts \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正規表現で指定 /b.*e/\n",
      "bcde\n",
      "\n",
      "slice!メソッド(範囲指定)\n",
      "I have an apple.\n",
      "hav\n",
      "I e an apple.\n",
      "\n",
      "slice!メソッド(文字列)\n",
      "I have a pineapple.\n",
      "e\n",
      "I hav a pineapple.\n",
      "\n",
      "slice!メソッド(正規表現 /pin.*e\\sa/)\n",
      "pen pineapple apple pen.\n",
      "pineapple a\n",
      "pen pple pen.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "puts '正規表現で指定 /b.*e/'\n",
    "puts \"abcdefg\"[/b.*e/]\n",
    "puts \"\"\n",
    "\n",
    "puts \"slice!メソッド(範囲指定)\"\n",
    "puts a = \"I have an apple.\"\n",
    "puts a.slice!(2..4)\n",
    "puts a\n",
    "puts \"\"\n",
    "\n",
    "puts \"slice!メソッド(文字列)\"\n",
    "puts b = \"I have a pineapple.\"\n",
    "puts b.slice!(\"e\")\n",
    "puts b\n",
    "puts \"\"\n",
    "\n",
    "puts 'slice!メソッド(正規表現 /pin.*e\\sa/)'\n",
    "puts c = \"pen pineapple apple pen.\"\n",
    "puts c.slice!(/pin.*e\\sa/)\n",
    "puts c\n",
    "puts \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "splitメソッド\n",
      "[\"ab_c\", \"_ef_gh\"]\n",
      "[\"ab_c\", \"_ef_gh\"]\n",
      "[\"ab_c\", \"_ef_\", \"h\"]\n",
      "\n",
      "第二引数が無ければセパレータですべて分割する\n",
      "[\"ab\", \"cd\", \"ef\", \"gh\"]\n",
      "\n",
      "第二引数で指定した数の要素数になるように先頭からセパレータで分割する\n",
      "[\"ab_cd_ef_gh\"]\n",
      "[\"ab\", \"cd_ef_gh\"]\n",
      "[\"ab\", \"cd\", \"ef_gh\"]\n"
     ]
    }
   ],
   "source": [
    "puts \"splitメソッド\"\n",
    "puts \"ab_cd_ef_gh\".split('d')\n",
    "puts \"ab_cd_ef_gh\".split(/d/)\n",
    "puts \"ab_cd_ef_gh\".split(/[dg]/)\n",
    "puts \"\"\n",
    "\n",
    "puts \"第二引数が無ければセパレータですべて分割する\"\n",
    "puts \"ab_cd_ef_gh\".split('_')\n",
    "puts \"\"\n",
    "\n",
    "puts \"第二引数で指定した数の要素数になるように先頭からセパレータで分割する\"\n",
    "puts \"ab_cd_ef_gh\".split('_',1)\n",
    "puts \"ab_cd_ef_gh\".split('_',2)\n",
    "puts \"ab_cd_ef_gh\".split('_',3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m string.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m String\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m split(*arg1)\n",
      "\u001b[1mNumber of lines:\u001b[0m 41\n",
      "\n",
      "Divides \u001b[1mstr\u001b[0m into substrings based on a delimiter, returning an array\n",
      "of these substrings.\n",
      "\n",
      "If \u001b[1mpattern\u001b[0m is a \u001b[1;34;4mString\u001b[0m, then its contents are used as\n",
      "the delimiter when splitting \u001b[1mstr\u001b[0m. If \u001b[1mpattern\u001b[0m is a single\n",
      "space, \u001b[1mstr\u001b[0m is split on whitespace, with leading whitespace and runs\n",
      "of contiguous whitespace characters ignored.\n",
      "\n",
      "If \u001b[1mpattern\u001b[0m is a \u001b[1;34;4mRegexp\u001b[0m, \u001b[1mstr\u001b[0m is divided where the\n",
      "pattern matches. Whenever the pattern matches a zero-length string,\n",
      "\u001b[1mstr\u001b[0m is split into individual characters. If \u001b[1mpattern\u001b[0m contains\n",
      "groups, the respective matches will be returned in the array as well.\n",
      "\n",
      "If \u001b[1mpattern\u001b[0m is \u001b[1;36mnil\u001b[0m, the value of \u001b[1;32m$;\u001b[0m is used.\n",
      "If \u001b[1;32m$;\u001b[0m is \u001b[1;36mnil\u001b[0m (which is the default), \u001b[1mstr\u001b[0m is\n",
      "split on whitespace as if ` ' were specified.\n",
      "\n",
      "If the \u001b[1mlimit\u001b[0m parameter is omitted, trailing null fields are\n",
      "suppressed. If \u001b[1mlimit\u001b[0m is a positive number, at most that number of\n",
      "fields will be returned (if \u001b[1mlimit\u001b[0m is \u001b[1;34m1\u001b[0m, the entire\n",
      "string is returned as the only entry in an array). If negative, there is no\n",
      "limit to the number of fields returned, and trailing null fields are not\n",
      "suppressed.\n",
      "\n",
      "When the input \u001b[32mstr\u001b[0m is empty an empty Array is returned as the string is\n",
      "considered to have no fields to split.\n",
      "\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m now's  the time\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.split        \u001b[1;34m#=> [\"now's\", \"the\", \"time\"]\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m now's  the time\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.split(\u001b[31m\u001b[1;31m'\u001b[0m\u001b[31m \u001b[1;31m'\u001b[0m\u001b[31m\u001b[0m)   \u001b[1;34m#=> [\"now's\", \"the\", \"time\"]\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m now's  the time\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.split(\u001b[35m\u001b[1;35m/\u001b[0m\u001b[35m \u001b[1;35m/\u001b[0m\u001b[35m\u001b[0m)   \u001b[1;34m#=> [\"\", \"now's\", \"\", \"the\", \"time\"]\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m1, 2.34,56, 7\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.split(\u001b[35m\u001b[1;35m%r{\u001b[0m\u001b[35m,\u001b[1;35m\\s\u001b[0m\u001b[35m*\u001b[1;35m}\u001b[0m\u001b[35m\u001b[0m) \u001b[1;34m#=> [\"1\", \"2.34\", \"56\", \"7\"]\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mhello\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.split(\u001b[35m\u001b[1;35m/\u001b[0m\u001b[35m\u001b[1;35m/\u001b[0m\u001b[35m\u001b[0m)               \u001b[1;34m#=> [\"h\", \"e\", \"l\", \"l\", \"o\"]\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mhello\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.split(\u001b[35m\u001b[1;35m/\u001b[0m\u001b[35m\u001b[1;35m/\u001b[0m\u001b[35m\u001b[0m, \u001b[1;34m3\u001b[0m)            \u001b[1;34m#=> [\"h\", \"e\", \"llo\"]\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mhi mom\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.split(\u001b[35m\u001b[1;35m%r{\u001b[0m\u001b[35m\u001b[1;35m\\s\u001b[0m\u001b[35m*\u001b[1;35m}\u001b[0m\u001b[35m\u001b[0m)         \u001b[1;34m#=> [\"h\", \"i\", \"m\", \"o\", \"m\"]\u001b[0m\n",
      "\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mmellow yellow\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.split(\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mello\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m)   \u001b[1;34m#=> [\"m\", \"w y\", \"w\"]\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m1,2,,3,4,,\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.split(\u001b[31m\u001b[1;31m'\u001b[0m\u001b[31m,\u001b[1;31m'\u001b[0m\u001b[31m\u001b[0m)         \u001b[1;34m#=> [\"1\", \"2\", \"\", \"3\", \"4\"]\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m1,2,,3,4,,\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.split(\u001b[31m\u001b[1;31m'\u001b[0m\u001b[31m,\u001b[1;31m'\u001b[0m\u001b[31m\u001b[0m, \u001b[1;34m4\u001b[0m)      \u001b[1;34m#=> [\"1\", \"2\", \"\", \"3,4,,\"]\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m1,2,,3,4,,\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.split(\u001b[31m\u001b[1;31m'\u001b[0m\u001b[31m,\u001b[1;31m'\u001b[0m\u001b[31m\u001b[0m, \u001b[1;34m-4\u001b[0m)     \u001b[1;34m#=> [\"1\", \"2\", \"\", \"3\", \"4\", \"\", \"\"]\u001b[0m\n",
      "\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.split(\u001b[31m\u001b[1;31m'\u001b[0m\u001b[31m,\u001b[1;31m'\u001b[0m\u001b[31m\u001b[0m, \u001b[1;34m-1\u001b[0m)               \u001b[1;34m#=> []\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "show-doc String#split"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 文字列の変更\n",
    "\n",
    "* 文字列の一部分を変更\n",
    "* メソッド\n",
    "    * []=\n",
    "    * insert\n",
    "        * indexで指定した場所の<font color=\"red\">手前に</font>文字列を挿入する"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "index1から3文字(bcd)を削除してxyを挿入\n",
      "abcdefg\n",
      "xy\n",
      "axyefg\n",
      "\n",
      "正規表現(/b.*f/)でマッチした部分を削除してxyを挿入\n",
      "abcdefg\n",
      "xy\n",
      "axyg\n",
      "\n",
      "insertメソッドでindex(3)(文字列d)の手前に文字列(xxx)を挿入する\n",
      "abcdefg\n",
      "abcxxxdefg\n",
      "\n"
     ]
    }
   ],
   "source": [
    "puts \"index1から3文字(bcd)を削除してxyを挿入\"\n",
    "puts a = \"abcdefg\"\n",
    "puts a[1..3] = 'xy'\n",
    "puts a\n",
    "puts \"\"\n",
    "\n",
    "puts \"正規表現(/b.*f/)でマッチした部分を削除してxyを挿入\"\n",
    "puts b = \"abcdefg\"\n",
    "puts b[/b.*f/] = 'xy'\n",
    "puts b\n",
    "puts \"\"\n",
    "\n",
    "puts \"insertメソッドでindex(3)(文字列d)の手前に文字列(xxx)を挿入する\"\n",
    "puts c = \"abcdefg\"\n",
    "puts c.insert(3, \"xxx\")\n",
    "puts \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12 X 34\n",
      "123 X 4\n"
     ]
    }
   ],
   "source": [
    "s = \"1234\"\n",
    "s.insert(2, \" X \")\n",
    "puts s\n",
    "t= \"1234\"\n",
    "t.insert(-2, \" X \")\n",
    "puts t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m string.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m String\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m insert(arg1, arg2)\n",
      "\u001b[1mNumber of lines:\u001b[0m 11\n",
      "\n",
      "Inserts \u001b[1mother_str\u001b[0m before the character at the given\n",
      "\u001b[1mindex\u001b[0m, modifying \u001b[1mstr\u001b[0m. Negative indices count from the\n",
      "end of the string, and insert \u001b[1mafter\u001b[0m the given character.\n",
      "The intent is insert \u001b[1maString\u001b[0m so that it starts at the given\n",
      "\u001b[1mindex\u001b[0m.\n",
      "\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mabcd\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.insert(\u001b[1;34m0\u001b[0m, \u001b[31m\u001b[1;31m'\u001b[0m\u001b[31mX\u001b[1;31m'\u001b[0m\u001b[31m\u001b[0m)    \u001b[1;34m#=> \"Xabcd\"\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mabcd\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.insert(\u001b[1;34m3\u001b[0m, \u001b[31m\u001b[1;31m'\u001b[0m\u001b[31mX\u001b[1;31m'\u001b[0m\u001b[31m\u001b[0m)    \u001b[1;34m#=> \"abcXd\"\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mabcd\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.insert(\u001b[1;34m4\u001b[0m, \u001b[31m\u001b[1;31m'\u001b[0m\u001b[31mX\u001b[1;31m'\u001b[0m\u001b[31m\u001b[0m)    \u001b[1;34m#=> \"abcdX\"\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mabcd\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.insert(\u001b[1;34m-3\u001b[0m, \u001b[31m\u001b[1;31m'\u001b[0m\u001b[31mX\u001b[1;31m'\u001b[0m\u001b[31m\u001b[0m)   \u001b[1;34m#=> \"abXcd\"\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mabcd\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.insert(\u001b[1;34m-1\u001b[0m, \u001b[31m\u001b[1;31m'\u001b[0m\u001b[31mX\u001b[1;31m'\u001b[0m\u001b[31m\u001b[0m)   \u001b[1;34m#=> \"abcdX\"\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "show-doc String#insert"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 文字列の置換"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### subメソッド, gsubメソッド\n",
    "\n",
    "* メソッド\n",
    "    * sub\n",
    "        * 指定したパターンにマッチした最初の部分を特定の文字列に置換する\n",
    "        * 文字列と正規表現が使える\n",
    "        * ブロックを取ることもでき、ブロックにマッチした部分がわたされブロックの実行結果と置換される\n",
    "    * gsub\n",
    "        * マッチしたすべての部分を置換する\n",
    "        * 文字列と正規表現が使える\n",
    "        * ブロックを取ることもでき、ブロックにマッチした部分がわたされブロックの実行結果と置換される\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "subは最初にマッチした部分、gsubはすべてを置換する\n",
      "abcdefg-abcdefg\n",
      "xyzdefg-abcdefg\n",
      "xyzdefg-xyzdefg\n",
      "\n",
      "ブロックを取る場合\n",
      "abcdefg-abcdefg\n",
      "xyzdefg-abcdefg\n",
      "xyzdefg-xyzdefg\n",
      "\n"
     ]
    }
   ],
   "source": [
    "puts \"subは最初にマッチした部分、gsubはすべてを置換する\"\n",
    "puts a = 'abcdefg-abcdefg'\n",
    "puts a.sub(/abc/, 'xyz')\n",
    "puts a.gsub(/abc/, 'xyz')\n",
    "puts \"\"\n",
    "\n",
    "puts \"ブロックを取る場合\"\n",
    "puts a = 'abcdefg-abcdefg'\n",
    "puts a.sub(/abc/) {|s| 'xyz'}\n",
    "puts a.gsub(/abc/) {|s| 'xyz'}\n",
    "puts \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdefg-abcdefg\n",
      "ydefg-ydefg\n"
     ]
    }
   ],
   "source": [
    "puts a = 'abcdefg-abcdefg'\n",
    "puts a.gsub(/abc/) {|s| 'xyz'[1] }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### trメソッド、tr_sメソッド\n",
    "\n",
    "* メソッド\n",
    "    * tr\n",
    "        * 指定したパターンに含まれる文字を検索し、特定の文字列やパターンに合わせて置換する\n",
    "    * tr_s\n",
    "        * 指定したパターンに含まれる文字を検索し、特定の文字列やパターンに合わせて置換し、重複する文字を1文字に圧縮する"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aabbccdadebefcfggaabbcc\n",
      "AABBCCdAdeBefCfggAABBCC\n",
      "ABCdAdeBefCfggABC\n"
     ]
    }
   ],
   "source": [
    "puts a = 'aabbccdadebefcfggaabbcc'\n",
    "puts a.tr('a-c','A-C')\n",
    "puts a.tr_s('a-c','A-C')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"HOGGefg\""
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 'abcdefg'\n",
    "a.tr('abcd','HOG')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### deleteメソッド\n",
    "\n",
    "* 指定したパターンに含まれる文字を元の文字列から削除する\n",
    "* パターンを複数指定すると、<font color=\"red\">すべてのパターンに含まれる文字列のみ削除する</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdefg-abcdefg\n",
      "bcdefg-bcdefg\n",
      "defg-defg\n",
      "eg-eg\n",
      "パターンとしてabcとbcdを指定。全てのパターンに含まれるbcが削除される\n",
      "adefg-adefg\n",
      "abcdefg-abcdefg\n"
     ]
    }
   ],
   "source": [
    "puts a = 'abcdefg-abcdefg'\n",
    "puts a.delete('a')\n",
    "puts a.delete('a-c')\n",
    "puts a.delete('a-df')\n",
    "\n",
    "puts 'パターンとしてabcとbcdを指定。全てのパターンに含まれるbcが削除される'\n",
    "puts a.delete('abc','bcd')\n",
    "puts a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m string.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m String\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m delete(*arg1)\n",
      "\u001b[1mNumber of lines:\u001b[0m 8\n",
      "\n",
      "Returns a copy of \u001b[1mstr\u001b[0m with all characters in the intersection of its\n",
      "arguments deleted. Uses the same rules for building the set of characters as\n",
      "\u001b[1;34;4mString\u001b[0m\u001b[1;34m#count\u001b[0m.\n",
      "\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mhello\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.delete \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31ml\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m,\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mlo\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m        \u001b[1;34m#=> \"heo\"\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mhello\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.delete \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mlo\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m            \u001b[1;34m#=> \"he\"\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mhello\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.delete \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31maeiou\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m, \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m^e\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m   \u001b[1;34m#=> \"hell\"\u001b[0m\n",
      "   \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mhello\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m.delete \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mej-m\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m          \u001b[1;34m#=> \"ho\"\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "show-doc String#delete"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### squeezeメソッド\n",
    "\n",
    "* メソッド\n",
    "    * squeeze\n",
    "        * 指定した文字列が複数並んでいた場合に一文字に圧縮する"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aabbccdadebefcfaaggbbcc\n",
      "abcdadebefcfaggbc\n",
      "aabbccdadebefcfaaggbbcc\n"
     ]
    }
   ],
   "source": [
    "puts a = 'aabbccdadebefcfaaggbbcc'\n",
    "puts a.squeeze('a-c')\n",
    "puts a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### replaceメソッド\n",
    "\n",
    "* メソッド\n",
    "    * 引数の文字列で<font color=\"red\">自分自身の内容を置き換える</font>\n",
    "        * 破壊的メソッド"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "asnfoiaeowef\n",
      "70109869060220\n",
      "xyz\n",
      "変数の値は変更されたまま\n",
      "xyz\n",
      "オブジェクトIDは変わらない\n",
      "70109869060220\n"
     ]
    }
   ],
   "source": [
    "puts a = 'asnfoiaeowef'\n",
    "puts a.object_id\n",
    "puts a.replace('xyz')\n",
    "puts \"変数の値は変更されたまま\"\n",
    "puts a\n",
    "puts \"オブジェクトIDは変わらない\"\n",
    "puts a.object_id"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 文字列の連結"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "* メソッド\n",
    "  * +\n",
    "      * 文字列を連結した新しいオブジェクトを作成する\n",
    "  * `<<`\n",
    "      * 元のオブジェクトに文字列を追記する\n",
    "      * 破壊的メソッド\n",
    "  * concat\n",
    "      * 元のオブジェクトに文字列を追記する\n",
    "      * 破壊的メソッド\n",
    "  * *\n",
    "      * 文字列の内容を指定した数値の数だけ繰り返した文字列を返す\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abc\n",
      "70336050517060\n",
      "\n",
      "abcxyz\n",
      "abc\n",
      "\n",
      "abcefg\n",
      "abcefg\n",
      "70336050517060\n",
      "\n",
      "abcefghij\n",
      "concatの引数に整数を入れるとそのASCIIコードの文字列が入る\n",
      "abcefghijA\n",
      "70336050517060\n",
      "\n",
      "abcefghijAabcefghijAabcefghijA\n",
      "abcefghijA\n"
     ]
    }
   ],
   "source": [
    "puts a = 'abc'\n",
    "puts a.object_id\n",
    "puts \"\"\n",
    "\n",
    "puts a + 'xyz'\n",
    "puts a\n",
    "puts \"\"\n",
    "\n",
    "puts a << \"efg\"\n",
    "puts a\n",
    "puts a.object_id\n",
    "puts \"\"\n",
    "\n",
    "puts a.concat(\"hij\")\n",
    "puts \"concatの引数に整数を入れるとそのASCIIコードの文字列が入る\"\n",
    "puts a.concat(65)\n",
    "puts a.object_id\n",
    "puts \"\"\n",
    "\n",
    "puts a * 3\n",
    "puts a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 文字列の大文字小文字変換\n",
    "\n",
    "* メソッド\n",
    "    * capitalize\n",
    "        * 文字列の先頭にある半角英字を大文字にして残りの半角英字を小文字にする\n",
    "    * downcase\n",
    "        * 半角英字を小文字にする\n",
    "    * upcase\n",
    "        * 半角英字を大文字にする\n",
    "    * swapcase\n",
    "        * 半角英字の大文字は小文字、小文字は大文字にする\n",
    "    * !をつけると破壊的メソッド"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fooExtMethods\n",
      "\n",
      "capitalize\n",
      "Fooextmethods\n",
      "\n",
      "downcase\n",
      "fooextmethods\n",
      "\n",
      "upcase\n",
      "FOOEXTMETHODS\n",
      "\n",
      "swapcase\n",
      "FOOeXTmETHODS\n",
      "\n"
     ]
    }
   ],
   "source": [
    "puts a = 'fooExtMethods'\n",
    "puts \"\"\n",
    "\n",
    "puts \"capitalize\"\n",
    "puts a.capitalize\n",
    "puts \"\"\n",
    "\n",
    "puts \"downcase\"\n",
    "puts a.downcase\n",
    "puts \"\"\n",
    "\n",
    "puts \"upcase\"\n",
    "puts a.upcase\n",
    "puts \"\"\n",
    "\n",
    "puts \"swapcase\"\n",
    "puts a.swapcase\n",
    "puts \"\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 文字列の末尾や先頭にある空白や改行を削除する\n",
    "\n",
    "* メソッド\n",
    "    * chomp\n",
    "        * 末尾から引数で指定する改行コードを取り除いた文字列を返す\n",
    "        * 指定が無い場合は全ての改行コード(\\n, \\r, \\r\\n)を削除する\n",
    "            * \\n\n",
    "                * LF 改行形式\n",
    "                * UNIXの改行コード\n",
    "            * \\r\n",
    "                * CR 改行形式\n",
    "                * Macの改行コード\n",
    "            * \\r\\n\n",
    "                * CR + LF 改行形式\n",
    "                * Windowsの改行コード\n",
    "        * 改行コードが無い場合は変化なし\n",
    "        * 引数指定\n",
    "            * 引数を指定すると、その文字列を<font color=\"red\">改行文字とみなす</font>\n",
    "            * <font color=\"red\">「\\n」の指定はデフォルトと同じ</font>ですべての改行コードが削除される\n",
    "\n",
    "    * chop\n",
    "        * 末尾の文字を取り除いた文字列を返す\n",
    "        * <font color=\"red\">末尾が改行コードの場合は改行コードを削除する</font>\n",
    "        * 空文字列の場合はそのまま空を返す\n",
    "            * エラーになったりnilになったりはしない\n",
    "    * strip\n",
    "        * 先頭と末尾の空白文字を<font color=\"red\">すべて</font>取り除く\n",
    "            * 空白文字\n",
    "                * \\t\n",
    "                    * タブ(水平タブ)\n",
    "                * \\r\n",
    "                    * 同じ行の先頭に戻る\n",
    "                * \\n\n",
    "                    * 改行\n",
    "                * \\f\n",
    "                    * ページ送り\n",
    "                * \\v\n",
    "                    * 垂直タブ\n",
    "    * lstrip\n",
    "        * 先頭の空白文字をすべて取り除く\n",
    "    * rstrip\n",
    "        * 末尾の空白文字をすべて取り除く\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "改行コードを削除\n",
      "\"unix\\n\"\n",
      "\"unix\"\n",
      "\n",
      "\"mac\\r\"\n",
      "\"mac\"\n",
      "\n",
      "\"windows\\r\\n\"\n",
      "\"windows\"\n",
      "\n",
      "一回実行するごとに一つの改行コードを削除する\n",
      "\"abc\\n\\r\\r\\n\"\n",
      "\"abc\\n\\r\"\n",
      "\"abc\\n\"\n",
      "\"abc\"\n",
      "\n",
      "「\\n」を指定すると引数無しと同じ削除\n",
      "\"abc\\r\\n\"\n",
      "\"abc\"\n",
      "\n",
      "「\\r」を指定すると「\\n」は削除されない\n",
      "\"abc\\n\"\n",
      "\"abc\\n\"\n",
      "\n",
      "引数に文字列を指定すると、その文字列を改行文字とみなして削除する\n",
      "\"abcdef\"\n",
      "\"ab\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"ab\""
      ]
     },
     "execution_count": 214,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "puts \"改行コードを削除\"\n",
    "p unix = \"unix\\n\"\n",
    "p unix.chomp\n",
    "puts \"\"\n",
    "\n",
    "p mac = \"mac\\r\"\n",
    "p mac.chomp\n",
    "puts \"\"\n",
    "\n",
    "p windows = \"windows\\r\\n\"\n",
    "p windows.chomp\n",
    "puts \"\"\n",
    "\n",
    "puts \"一回実行するごとに一つの改行コードを削除する\"\n",
    "p a = \"abc\\n\\r\\r\\n\"\n",
    "p a.chomp\n",
    "p a.chomp.chomp\n",
    "p a.chomp.chomp.chomp\n",
    "puts \"\"\n",
    "\n",
    "puts '「\\n」を指定すると引数無しと同じ削除'\n",
    "p a = \"abc\\r\\n\"\n",
    "p a.chomp(\"\\n\")\n",
    "puts \"\"\n",
    "\n",
    "puts '「\\r」を指定すると「\\n」は削除されない'\n",
    "p a = \"abc\\n\"\n",
    "p a.chomp(\"\\r\")\n",
    "puts \"\"\n",
    "\n",
    "\n",
    "puts '引数に文字列を指定すると、その文字列を改行文字とみなして削除する'\n",
    "p a = \"abcdef\"\n",
    "p a.chomp(\"cdef\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chopで末尾を削除する\n",
      "\"ab\\n\\r\\r\\n\"\n",
      "\"ab\\n\\r\"\n",
      "\"ab\\n\"\n",
      "\"ab\"\n",
      "\"a\"\n",
      "\"\"\n",
      "\"\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"\""
      ]
     },
     "execution_count": 222,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "puts \"chopで末尾を削除する\"\n",
    "p a = \"ab\\n\\r\\r\\n\"\n",
    "p a.chop\n",
    "p a.chop.chop\n",
    "p a.chop.chop.chop\n",
    "p a.chop.chop.chop.chop\n",
    "p a.chop.chop.chop.chop.chop\n",
    "p a.chop.chop.chop.chop.chop.chop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"\\t\\rabc\\f\\v\"\n",
      "\"abc\"\n",
      "\"abc\\f\\v\"\n",
      "\"\\t\\rabc\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"\\t\\rabc\""
      ]
     },
     "execution_count": 228,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p a = \"\\t\\rabc\\f\\v\"\n",
    "p a.strip\n",
    "p a.lstrip\n",
    "p a.rstrip"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 文字列を逆順にする\n",
    "\n",
    "* メソッド\n",
    "    * reverse\n",
    "        * 文字列を逆順にする"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aBcdEf\n",
      "fEdcBa\n",
      "aBcdEf\n"
     ]
    }
   ],
   "source": [
    "puts a = \"aBcdEf\"\n",
    "puts a.reverse\n",
    "puts a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 文字列の長さを求める\n",
    "\n",
    "* メソッド\n",
    "    * length\n",
    "        * 文字数を数える\n",
    "        * Ruby1.8まではバイト数を返していた\n",
    "    * size\n",
    "        * 文字数を数える\n",
    "        * Ruby1.8まではバイト数を返していた\n",
    "        * lengthと差は無い？\n",
    "    * bytesize\n",
    "        * バイト数が返る\n",
    "    * count\n",
    "        * 指定されたパターンに該当する文字がいくつあるかを数える\n",
    "    * empty?\n",
    "        * 文字列が空かどうかを返す\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 238,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdef\n",
      "6\n",
      "6\n",
      "6\n",
      "\n",
      "私はJimです。\n",
      "8\n",
      "8\n",
      "18\n",
      "\n"
     ]
    }
   ],
   "source": [
    "puts a = \"abcdef\"\n",
    "puts a.length\n",
    "puts a.size\n",
    "puts a.bytesize\n",
    "puts \"\"\n",
    "\n",
    "puts b = \"私はJimです。\"\n",
    "puts b.length\n",
    "puts b.size\n",
    "puts b.bytesize\n",
    "puts \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 250,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdef-abcdef\n",
      "aは2つある\n",
      "2\n",
      "aとbは合わせて4つある。パターンの指定はabもbaも同じ\n",
      "4\n",
      "4\n",
      "c-e(cde)は6つある\n",
      "6\n",
      "\n",
      "文字列が空かどうかを調査\n",
      "true\n",
      "false\n"
     ]
    }
   ],
   "source": [
    "puts a = \"abcdef-abcdef\"\n",
    "puts \"aは2つある\"\n",
    "puts a.count('a')\n",
    "puts \"aとbは合わせて4つある。パターンの指定はabもbaも同じ\"\n",
    "puts a.count('ab')\n",
    "puts a.count('ba')\n",
    "puts \"c-e(cとdとe)は6つある\"\n",
    "puts a.count('c-e')\n",
    "puts \"\"\n",
    "\n",
    "puts \"文字列が空かどうかを調査\"\n",
    "b = \"\"\n",
    "c = \"c\"\n",
    "puts b.empty?\n",
    "puts c.empty?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 文字列の割り付け\n",
    "\n",
    "* 文字列をある長さの文字列の中に割り付ける\n",
    "    * デフォルトは空白\n",
    "    * 第二引数\n",
    "        * 文字列を1つ指定した場合はその文字が割り付けられる\n",
    "        * 文字列を複数指定した場合はその文字列の並び順で割り当てられる\n",
    "        * あふれた場合は無視される\n",
    "* メソッド\n",
    "    * center\n",
    "        * 中央に割り付ける\n",
    "        * 割り付ける順番\n",
    "            * まず後ろに割り付ける\n",
    "            * 次に前に割り付ける\n",
    "            * 後、前、後、前\n",
    "    * ljust\n",
    "        * 左側に割り付ける\n",
    "            * デフォルトで<font color=\"red\">右側(末尾)に</font>空白が入る\n",
    "    * rjust\n",
    "        * 右側に割り付ける\n",
    "            * デフォルトで<font color=\"red\">左側(先頭)に</font>空白が入る\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 279,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"abc\"\n",
      "\n",
      "指定した文字数になるように前後に空白を入れる\n",
      "\"abc \"\n",
      "\" abc \"\n",
      "\" abc  \"\n",
      "\"  abc  \"\n",
      "7\n",
      "\n",
      "空白以外を割り付ける。あふれたところは無視される\n",
      "\"AAAabcAAAA\"\n",
      "\"ABAabcABAB\"\n",
      "\"ABCabcABCA\"\n",
      "\"ABCabcABCD\"\n",
      "\"ABCabcABCD\"\n",
      "\n",
      "ljustは右側、rjustは左側に空白が入る\n",
      "\"[ljust]abc  \"\n",
      "\"[rjust]  abc\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"[rjust]  abc\""
      ]
     },
     "execution_count": 279,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p a = \"abc\"\n",
    "puts \"\"\n",
    "\n",
    "puts \"指定した文字数になるように前後に空白を入れる\"\n",
    "p a.center(4)\n",
    "p a.center(5)\n",
    "p a.center(6)\n",
    "p a.center(7)\n",
    "puts a.center(7).length\n",
    "puts \"\"\n",
    "\n",
    "puts \"空白以外を割り付ける。あふれたところは無視される\"\n",
    "p a.center(10,\"A\")\n",
    "p a.center(10,\"AB\")\n",
    "p a.center(10,\"ABC\")\n",
    "p a.center(10,\"ABCD\")\n",
    "p a.center(10,\"ABCDE\")\n",
    "puts \"\"\n",
    "\n",
    "puts \"ljustは右側、rjustは左側に空白が入る\"\n",
    "p \"[ljust]\" + a.ljust(5)\n",
    "p \"[rjust]\" + a.rjust(5)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 非表示文字列を変換する\n",
    "\n",
    "* メソッド\n",
    "    * dump\n",
    "        * 改行コードやタブをバックスラッシュ記法に置き換えた文字列を返す"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abc\f",
      "\u000b",
      "\n",
      "\"\\t\\rabc\\f\\v\"\n"
     ]
    }
   ],
   "source": [
    "a = \"\\t\\rabc\\f\\v\"\n",
    "puts a\n",
    "puts a.dump"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 文字列をアンパックする\n",
    "\n",
    "* メソッド\n",
    "    * unpack\n",
    "        * Array#packメソッドでパックされた文字列を指定したテンプレートに従ってアンパックする\n",
    "        * MIMEエンコードされた文字列のデコードに利用\n",
    "        * バイナリデータから数値への変換に利用\n",
    "        * <font color=\"red\">アンパックされた文字列のエンコーディングはASCII-8BIT</font>\n",
    "            * 必要に応じてエンコーディングを設定する\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MIMEエンコードされた文字列\n",
      "44Or44OT44O8\n",
      "\n",
      "アンパックする(エンコーディングはASCII-8BIT)\n",
      "[\"\\xE3\\x83\\xAB\\xE3\\x83\\x93\\xE3\\x83\\xBC\"]\n",
      "\n",
      "アンパックされた配列の要素をUTF-8でエンコーディングする\n",
      "ASCII-8BIT\n",
      "ルビー\n"
     ]
    }
   ],
   "source": [
    "puts \"MIMEエンコードされた文字列\"\n",
    "puts a = [\"ルビー\"].pack(\"m\")\n",
    "\n",
    "puts \"アンパックする(エンコーディングはASCII-8BIT)\"\n",
    "puts a.unpack('m')\n",
    "puts \"\"\n",
    "\n",
    "puts \"アンパックされた配列の要素をUTF-8でエンコーディングする\"\n",
    "puts a.unpack('m').first.encoding\n",
    "puts a.unpack('m').first.force_encoding('UTF-8')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 文字列内での検索\n",
    "\n",
    "* メソッド\n",
    "    * include?\n",
    "        * 指定された文字列が含まれているとtrueを返す\n",
    "    * index\n",
    "        * 指定された位置から右方向に検索し、最初に見つかった位置を返す\n",
    "        * 文字、文字コードの数値、正規表現のパターンを指定可能\n",
    "    * rindex\n",
    "        * 指定された位置から左方向に検索し、最初に見つかった位置を返す\n",
    "        * 文字、文字コードの数値、正規表現のパターンを指定可能\n",
    "    * match\n",
    "        * 指定された正規表現でマッチングをし、マッチした場合はMatchDataオブジェクトを返す\n",
    "    * scan\n",
    "        * 指定された正規表現でマッチングをし、マッチした部分文字列の配列を返す\n",
    "        * ブロックを渡すこともできる\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 312,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdef-abcdef\n",
      "\n",
      "include?メソッド\n",
      "true\n",
      "false\n",
      "\n",
      "indexメソッド\n",
      "2\n",
      "検索開始位置を指定\n",
      "2\n",
      "2\n",
      "9\n",
      "(ヒットしない) \n",
      "9\n",
      "9\n",
      "\n",
      "rindexメソッド\n",
      "9\n",
      "検索開始位置を指定\n",
      "(ヒットしない) \n",
      "2\n",
      "2\n",
      "9\n",
      "9\n",
      "2\n",
      "\n"
     ]
    }
   ],
   "source": [
    "puts a = \"abcdef-abcdef\"\n",
    "puts \"\"\n",
    "\n",
    "puts \"include?メソッド\"\n",
    "puts a.include?(\"ab\")\n",
    "puts a.include?(\"ba\")\n",
    "puts \"\"\n",
    "\n",
    "puts \"indexメソッド\"\n",
    "puts a.index(\"cd\")\n",
    "puts \"検索開始位置を指定\"\n",
    "puts a.index(\"cd\", 1)\n",
    "puts a.index(\"cd\", 2)\n",
    "puts a.index(\"cd\", 3)\n",
    "puts \"(ヒットしない) #{a.index(\"cd\", -3)}\"\n",
    "puts a.index(\"cd\", -4)\n",
    "puts a.index(\"cd\", -5)\n",
    "puts \"\"\n",
    "\n",
    "puts \"rindexメソッド\"\n",
    "puts a.rindex(\"cd\")\n",
    "puts \"検索開始位置を指定\"\n",
    "puts \"(ヒットしない) #{a.rindex(\"cd\", 1)}\"\n",
    "puts a.rindex(\"cd\", 2)\n",
    "puts a.rindex(\"cd\", 3)\n",
    "puts a.rindex(\"cd\", -3)\n",
    "puts a.rindex(\"cd\", -4)\n",
    "puts a.rindex(\"cd\", -5)\n",
    "puts \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdef-abcdef\n",
      "\n",
      "matchで指定した正規表現(/c.*ab/)でマッチした部分をMatchDataオブジェクトとして返す\n",
      "#<MatchData \"cdef-ab\">\n",
      "\n",
      "cdef-ab\n",
      "ab\n",
      "cdef-ab\n",
      "cdef\n",
      "\n",
      "ab\n",
      "\n",
      "ab\n",
      "cdef-abcdef\n"
     ]
    }
   ],
   "source": [
    "puts a = \"abcdef-abcdef\"\n",
    "puts \"\"\n",
    "\n",
    "puts \"matchで指定した正規表現(/c.*ab/)でマッチした部分をMatchDataオブジェクトとして返す\"\n",
    "#print a.match(/c.*ab/).inspect\n",
    "p a.match(/c.*ab/)\n",
    "puts \"\"\n",
    "puts a.match(/c.*ab/)\n",
    "puts $`\n",
    "puts $&\n",
    "puts $'\n",
    "puts\n",
    "\n",
    "puts a.match(/ab/)\n",
    "puts $`\n",
    "puts $&\n",
    "puts $'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdef-abcdef\n",
      "\n",
      "scanで指定した正規表現(/c.*ab/)でマッチした部分を配列として返す\n",
      "[\"cdef-ab\"]\n",
      "[\"ab\", \"ab\"]\n",
      "[\"a\", \"b\", \"c\", \"a\", \"b\", \"c\"]\n",
      "[\"abcdef-abc\"]\n",
      "[\"abc\", \"def\", \"-ab\", \"cde\"]\n",
      "[[\"abc\"], [\"def\"], [\"-ab\"], [\"cde\"]]\n",
      "cde\n",
      "cde\n",
      "\n",
      "\n",
      "[[\"abc\", \"def\"], [\"-ab\", \"cde\"]]\n",
      "-abcde\n",
      "-ab\n",
      "cde\n"
     ]
    }
   ],
   "source": [
    "puts a = \"abcdef-abcdef\"\n",
    "puts \"\"\n",
    "\n",
    "puts \"scanで指定した正規表現(/c.*ab/)でマッチした部分を配列として返す\"\n",
    "puts a.scan(/c.*ab/)\n",
    "puts a.scan(/ab/)\n",
    "puts a.scan(/[a-c]/)\n",
    "puts a.scan(/a.*c/)\n",
    "puts a.scan(/.../)\n",
    "puts a.scan(/(...)/)\n",
    "puts $~[0]\n",
    "puts $~[1]\n",
    "puts $~[2]\n",
    "puts\n",
    "\n",
    "puts a.scan(/(...)(...)/)\n",
    "puts $~[0]\n",
    "puts $~[1]\n",
    "puts $~[2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 次の文字列を求める\n",
    "\n",
    "* メソッド\n",
    "    * succ\n",
    "        * 次の文字列を求める\n",
    "            * アルファベット\n",
    "                * アルファベット順\n",
    "            * 数値\n",
    "                * 10進数の順\n",
    "    * next\n",
    "        * succと全く同じ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 360,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abd\n",
      "124\n",
      "xza\n",
      "1000\n",
      "zzzzy\n",
      "zzzzz\n",
      "aaaaaa\n"
     ]
    }
   ],
   "source": [
    "puts \"abc\".succ\n",
    "puts \"123\".succ\n",
    "puts \"xyz\".succ\n",
    "puts \"999\".succ\n",
    "puts \"zzzzx\".succ\n",
    "puts \"zzzzy\".succ\n",
    "puts \"zzzzz\".succ"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 文字列に対する繰り返し\n",
    "\n",
    "* メソッド\n",
    "    * each_line\n",
    "        * 文字列の各行に対して繰り返す\n",
    "        * 行の区切りを指定できる\n",
    "    * lines\n",
    "        * 文字列の各行に対して繰り返す\n",
    "        * 行の区切りを指定できる\n",
    "    * each_byte\n",
    "        * 文字列をバイト単位で繰り返す\n",
    "    * bytes\n",
    "        * 文字列をバイト単位で繰り返す\n",
    "    * each_char\n",
    "        * 文字列を文字単位で繰り返す\n",
    "    * chars\n",
    "        * 文字列を文字単位で繰り返す\n",
    "    * upto\n",
    "        * 自分自身から指定された文字列までsuccメソッドで生成される次の文字列を使って繰り返す\n",
    "    * 「each～」と「～s」は差はない?\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 385,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "行単位の繰り返し\n",
      "\"abc\\ndef\\nghi\"\n",
      "\n",
      "\"abc\\n\"\n",
      "\"def\\n\"\n",
      "\"ghi\"\n",
      "\"abc\\n\"\n",
      "\"def\\n\"\n",
      "\"ghi\"\n",
      "\n",
      "バイト単位の繰り返し\n",
      "\"abc\"\n",
      "\n",
      "97\n",
      "98\n",
      "99\n",
      "97\n",
      "98\n",
      "99\n",
      "\n",
      "文字列単位の繰り返し\n",
      "私はJim\n",
      "\n",
      "私\n",
      "は\n",
      "J\n",
      "i\n",
      "m\n",
      "私\n",
      "は\n",
      "J\n",
      "i\n",
      "m\n",
      "\n",
      "uptoメソッド\n",
      "a\n",
      "b\n",
      "c\n",
      "d\n",
      "y -> z -> a -> b のようには使えない\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"y\""
      ]
     },
     "execution_count": 385,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "puts \"行単位の繰り返し\"\n",
    "p a = \"abc\\ndef\\nghi\"\n",
    "puts \"\"\n",
    "\n",
    "a.each_line{|l| p l}\n",
    "a.lines{|l| p l}\n",
    "puts \"\"\n",
    "\n",
    "puts \"バイト単位の繰り返し\"\n",
    "p b = \"abc\"\n",
    "puts \"\"\n",
    "\n",
    "b.each_byte{|b| p b}\n",
    "b.bytes{|b| p b}\n",
    "puts \"\"\n",
    "\n",
    "puts \"文字列単位の繰り返し\"\n",
    "puts c = \"私はJim\"\n",
    "puts \"\"\n",
    "\n",
    "c.each_char{|c| puts c}\n",
    "c.chars{|c| puts c}\n",
    "puts \"\"\n",
    "\n",
    "puts \"uptoメソッド\"\n",
    "\"a\".upto(\"d\") {|c| puts c}\n",
    "puts \"y -> z -> a -> b のようには使えない\"\n",
    "\"y\".upto(\"b\") {|c| puts c}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 他のクラスへの変換\n",
    "\n",
    "* メソッド\n",
    "    * hex\n",
    "        * 文字列を16進数とみなして数値に変換\n",
    "            * 無視する\n",
    "                * 接頭 0x or 0X\n",
    "                * アンダースコア\n",
    "                    * アンダースコアの場合はその後ろも評価される\n",
    "                        * 0x01_0 は 0x010 と評価される\n",
    "                * 16進数以外の文字\n",
    "                    * 文字列の場合はそこまでを16進数と評価する\n",
    "                        * 0x01q0 は 0x01 と評価される\n",
    "    * oct\n",
    "        * デフォルトは文字列を8進数とみなして数値に変換\n",
    "            * 接頭に応じて8進数以外にも変換する\n",
    "                * 0x 16進数\n",
    "                *  0 8進数\n",
    "                * 0b 2進数\n",
    "    * to_i\n",
    "        * 文字列を10進数の整数に変換\n",
    "        * 整数とみなせない文字があればそこまでを変換して以降は無視\n",
    "        * <font color=\"red\">空文字の場合は0を返す</font>\n",
    "    * to_f\n",
    "        * 文字列を10進数の小数とみなしてFloatオブジェクトに変換\n",
    "        * 小数とみなせない文字があればそこまでを変換して以降は無視\n",
    "        * 空文字の場合は0.0を返す\n",
    "    * to_s\n",
    "        * 自分自身を返す\n",
    "    * to_str\n",
    "        * 自分自身を返す\n",
    "    * to_sym\n",
    "        * 文字列に対応するシンボルを返す\n",
    "        * <font color=\"red\">空文字やヌル文字(\\0)を含む文字列の場合はArgumentErrorが発生する</font>\n",
    "            * ⇒してない？\n",
    "    * intern\n",
    "        * 文字列に対応するシンボルを返す\n",
    "        * 空文字やヌル文字(\\0)を含む文字列の場合はArgumentErrorが発生する\n",
    "            * ⇒してない？\n",
    "        * to_symと同じ？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 415,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n",
      "16\n",
      "16\n",
      "\n",
      "後ろにあるg以降のアルファベット(16進数はfまで使う)や文字列は無視される\n",
      "16\n",
      "gの後ろの0が無視された結果1になる\n",
      "1\n",
      "\n",
      "0xは省略できる\n",
      "10\n",
      "10\n",
      "\n",
      "octで進数変換\n",
      "8\n",
      "16\n",
      "8\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "puts \"0x00F\".hex\n",
    "puts \"0x010\".hex\n",
    "puts \"0x01_0\".hex\n",
    "puts \"\"\n",
    "\n",
    "puts \"後ろにあるg以降のアルファベット(16進数はfまで使う)や文字列は無視される\"\n",
    "puts \"0x010j*/%&ほげぇ\".hex\n",
    "puts \"gの後ろの0が無視された結果1になる\"\n",
    "puts \"0x01g0\".hex\n",
    "puts \"\"\n",
    "\n",
    "puts \"0xは省略できる\"\n",
    "puts \"a\".hex\n",
    "puts \"az02\".hex\n",
    "puts \"\"\n",
    "\n",
    "puts \"octで進数変換\"\n",
    "puts \"10\".oct\n",
    "puts \"0x10\".oct\n",
    "puts \"010\".oct\n",
    "puts \"0b10\".oct\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 439,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fixnum\n",
      "Float\n",
      "String\n",
      "String\n",
      "Symbol\n",
      "Symbol\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       ":\"a b\""
      ]
     },
     "execution_count": 439,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "puts \"10\".to_i.class\n",
    "puts \"10\".to_f.class\n",
    "puts \"10\".to_s.class\n",
    "puts \"10\".to_str.class\n",
    "puts \"10\".to_sym.class\n",
    "puts \"10\".intern.class\n",
    "a = \"a b\"\n",
    "a.to_sym"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.3.1",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.3.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
